/** ***********************************************************************************
* @file         bluetooth_app.c
* @brief        蓝牙操作部分
* @details      
* @author       
* @date         2021/3
* @copyright    北京高知科技有限公司    
* @version      V1.0
* @attention    无
**************************************************************************************/

#define OSI_LOG_TAG OSI_MAKE_LOG_TAG('M', 'B', 'L', 'E')
#include "fibo_opencpu.h"
#include "rsa_driver.h"
#include "files.h"
#include "gaoz_api.h"
#include "algorithm.h"
#include "vehicle_data_process.h"
#include "n808_app.h"
#include "bluetooth_app.h"

#define BT_SEND_NAME    "BT_SEND_TASK"
#define BT_RECV_NAME    "BT_RECV_TASK"

bluetooth_recv_t	tagBT = {0};/*蓝牙属性*/
GaozQueueHandle_t bluetooth_ctlock_queue;    /*bluetooth控锁消息队列*/
GaozSemaphoreHandle_t   bluetooth_send_data_mutex = 0; //发送互斥

/** ****************************************************************************
* @remarks       void bluetooth_send_data_mutex_create( void )
* @brief         资源初始化
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     在系统启动后进行创建
*******************************************************************************/
static void bluetooth_send_data_mutex_create( void )
{
    /*创建任务互斥体*/
    bluetooth_send_data_mutex = gaoz_mutex_create();
    if( bluetooth_send_data_mutex == 0 )
    {
        while(1); /*创建失败*/
    }
}
/** ****************************************************************************
* @remarks        void bluetooth_recv_state_cb( void * pv)
* @brief         底层状态回调函数
* @param[in]     * pv 参数输入
* @param[out]    无
* @return        无
* @attention     
*******************************************************************************/
void bluetooth_recv_state_cb(uint8_t state )
{
	uint32_t	bluetooth_recv_st;

	bluetooth_recv_st = state;
	//gaoz_log_debug_printf(INFOR_MODE,"mble","bluetooth_recv_st:%d\r\n",bluetooth_recv_st);	
	switch(bluetooth_recv_st)
	{
		case TASK_MODE_INIT:
			
			break;
		case TASK_RUN_MODE:
			
			break;
		case TASK_SLEEP_MODE:
			
			break;
		case TASK_STOP_MODE:

			break;
		default:
			break;
	}

}
/** ****************************************************************************
* @remarks       uint8_t bluetooth_setrsa(uint8_t * pDa, uint16_t len)
* @brief         设置蓝牙RSA秘钥。
* @param[in]     *pDa缓存区，len长度
* @param[out]    无
* @return        0成功，1失败
* @attention     
*******************************************************************************/
uint8_t bluetooth_setrsa(uint8_t * pDa, uint16_t len)
{
	if ((len > BT_PUBKEYMAXLEN) || (len == 0) || (pDa == NULL))
	{
		return 1;
	}
	memset(tagBT.RSA_PubicKey,0,sizeof(tagBT.RSA_PubicKey));
	memcpy(tagBT.RSA_PubicKey, pDa, len);
	tagBT.RSA_PubicKeyLen = len;
	
	return 0;
}
/** ****************************************************************************
* @remarks       uint8_t bluetooth_getrsa(void)
* @brief        获取蓝牙rsa
* @param[in]     无
* @param[out]    无
* @return        缓存区指针
* @attention     
*******************************************************************************/
uint8_t *bluetooth_getrsa(void)
{
	if ((tagBT.RSA_PubicKeyLen > BT_PUBKEYMAXLEN) || (tagBT.RSA_PubicKey == NULL) || (tagBT.RSA_PubicKeyLen == 0))
	{
		return NULL;
	}
	return tagBT.RSA_PubicKey;
}
/** ****************************************************************************
* @remarks       uint8_t bluetooth_setname(uint8_t * pDa, uint16_t len)
* @brief         设置蓝牙名称
* @param[in]     * pDa缓存区，len长度
* @param[out]    无
* @return        0成功，1失败, 2其他错误
* @attention     
*******************************************************************************/
uint8_t bluetooth_setname(uint8_t * pDa, uint8_t len)
{
	
	if ((len > BT_NAMEMAXLEN) || (len == 0) || (pDa == NULL))
	{
		return 1;
	}
	
	if (chk_valid_ASC_name(pDa, len) != 0)
	{
		return 2;
	}
	memset(tagBT.bluetooth_name,0,sizeof(tagBT.bluetooth_name));
	memcpy(tagBT.bluetooth_name, pDa, len);
	tagBT.bluetooth_nameLen = (uint8_t)len;	

	return 0;
}
/** ****************************************************************************
* @remarks       uint8_t bluetooth_getname(void)
* @brief        获取蓝牙名称
* @param[in]     无
* @param[out]    无
* @return        缓存区指针
* @attention     
*******************************************************************************/
uint8_t *bluetooth_getname(void)
{
	
	if ((tagBT.bluetooth_nameLen > BT_NAMEMAXLEN) || (tagBT.bluetooth_nameLen == 0) || (tagBT.bluetooth_name == NULL))
	{
		return NULL;
	}
	
	return tagBT.bluetooth_name;
}
/** ****************************************************************************
* @remarks       uint32_t rsa_decipherdata(uint8_t *pDa, uint8_t lent)
* @brief         RSA解密以后处理的报文；1464710400|013|S|00000000|62
* @param[in]     *pDa缓存区，lent长度
* @param[out]    无
* @return       1失败 0成功 
* @attention     
*******************************************************************************/
uint32_t rsa_decipherdata(uint8_t *pDa, uint8_t lent)
{
	uint8_t cmd;
	uint32_t AppTime;		//app下发的时间，秒，从2000年1月1号0时0分0秒开始
	uint32_t deviceTime;	//设备本地时间，秒
	
	national808_ctrllock_msg_t  ctlock_msg;
	int ret = 0;

	if (lent > 28)
	{
		send_rsa_error(5);
		return 1;
	}

	gaoz_log_debug_printf(INFOR_MODE,"mble","Rsa_decipherData:%s\r\n", pDa);

	DecASCII2HexDWORD(pDa, 10, &AppTime);

	deviceTime = osiEpochSecond();

	if (AppTime <= (deviceTime-946656000))//2000 1 -1 0:0:0
	{
		gaoz_log_debug_printf(INFOR_MODE,"mble","bluetooth time error\r\n");
		send_rsa_error(0x03);
		return 1;
	}
	
	cmd = (pDa[25] - 0x30) * 10 + (pDa[26] - 0x30);
	gaoz_log_debug_printf(INFOR_MODE,"mble","cmd:%d\r\n",cmd);
	gaoz_power_man_wakeup_task_num_run(); /* wake up*/ 
	switch (cmd)
	{
	case 82:	//车门解锁只解锁
		if( (national808_ctrllock_state_get() == CTLOCK_INIT) ||
			(national808_ctrllock_state_get() == CTLOCK_OK))
		{
			national808_ctrllock_state_set( CTLOCK_BLE_UNLOCK);
			if(vehiclect_runing_state_get() == CARCTL_RUNING_INIT)
			{
				ctlock_msg.lock_type = 0x82;
				ctlock_msg.lock_res.respondcmd = 0;
				ctlock_msg.lock_res.respondsn = 0;
				ctlock_msg.lock_res.respondsta = 0; 
				ctlock_msg.lock_err = 0;
				ret = national808_ctlock_queue_put(&ctlock_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"mble","ctlock_queue_put bluetooth-82 fail!\r\n");
					national808_ctrllock_state_set( CTLOCK_OK);
				}
				else
				{
					if(vehiclect_runing_state_get() == CARCTL_RUNING_INIT)
					{
						vehiclect_runing_state_set(CARCTL_RUNING_LOCK_UNLOCK);/*控锁状态*/
					}
					else
					{
						national808_ctrllock_state_set(CTLOCK_OK);
					}
				}
			}
			else
			{
				national808_ctrllock_state_set(CTLOCK_OK);
			}
			
		}
		break;

	case 62:	//车门解锁，继电器上电
		if( (national808_ctrllock_state_get() == CTLOCK_INIT) ||
			(national808_ctrllock_state_get() == CTLOCK_OK))
		{
			national808_ctrllock_state_set(CTLOCK_BLE_UNLOCK);
			if(vehiclect_runing_state_get() == CARCTL_RUNING_INIT)
			{
				ctlock_msg.lock_type = 0x62;
				ctlock_msg.lock_res.respondcmd = 0;
				ctlock_msg.lock_res.respondsn = 0;
				ctlock_msg.lock_res.respondsta = 0; 
				ctlock_msg.lock_err = 0;
				ret = national808_ctlock_queue_put(&ctlock_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"mble","ctlock_queue_put bluetooth-62 fail!\r\n");
					national808_ctrllock_state_set(CTLOCK_OK);
				}
				else
				{
					if(vehiclect_runing_state_get() == CARCTL_RUNING_INIT)
					{
						vehiclect_runing_state_set(CARCTL_RUNING_LOCK_UNLOCK);/*控锁状态*/
						setrelay_sta(1);
					}
					else
					{
						national808_ctrllock_state_set(CTLOCK_OK);
					}
				}
			}
			else
			{
				national808_ctrllock_state_set(CTLOCK_OK);
			}
		}
		break;

	case 83:	//车门上锁，仅上锁
		if( (national808_ctrllock_state_get() == CTLOCK_INIT) ||
			(national808_ctrllock_state_get() == CTLOCK_OK))
		{
			national808_ctrllock_state_set(CTLOCK_BLE_LOCK);
			if(vehiclect_runing_state_get() == CARCTL_RUNING_INIT)
			{
				ctlock_msg.lock_type = 0x83;
				ctlock_msg.lock_res.respondcmd = 0;
				ctlock_msg.lock_res.respondsn = 0;
				ctlock_msg.lock_res.respondsta = 0; 
				ctlock_msg.lock_err = 0;
				ret = national808_ctlock_queue_put(&ctlock_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"mble","ctlock_queue_put bluetooth-83 fail!\r\n");
					national808_ctrllock_state_set(CTLOCK_OK);
				}
				else
				{
					if(vehiclect_runing_state_get() == CARCTL_RUNING_INIT)
					{
						vehiclect_runing_state_set(CARCTL_RUNING_LOCK_UNLOCK);/*控锁状态*/
					}
					else
					{
						national808_ctrllock_state_set(CTLOCK_OK);
					}
				}	
			}
			else
			{
				national808_ctrllock_state_set(CTLOCK_OK);
			}
		}
		break;

	case 63:	//车门上锁+断电
		if( (national808_ctrllock_state_get() == CTLOCK_INIT) ||
			(national808_ctrllock_state_get() == CTLOCK_OK))
		{
			national808_ctrllock_state_set(CTLOCK_BLE_LOCK);
			if(vehiclect_runing_state_get() == CARCTL_RUNING_INIT)
			{
				ctlock_msg.lock_type = 0x63;
				ctlock_msg.lock_res.respondcmd = 0;
				ctlock_msg.lock_res.respondsn = 0;
				ctlock_msg.lock_res.respondsta = 0; 
				ctlock_msg.lock_err = 0;
				ret = national808_ctlock_queue_put(&ctlock_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"mble","ctlock_queue_put bluetooth-63 fail!\r\n");
					national808_ctrllock_state_set(CTLOCK_OK);
				}
				else
				{
					if(vehiclect_runing_state_get() == CARCTL_RUNING_INIT)
					{
						vehiclect_runing_state_set(CARCTL_RUNING_LOCK_UNLOCK);/*控锁状态*/
						setrelay_sta(2);
					}
					else
					{
						national808_ctrllock_state_set(CTLOCK_OK);
					}
				}
			}
			else
			{
				national808_ctrllock_state_set(CTLOCK_OK);
			}
		}
		break;

	case 60:	//鸣笛
		if( (national808_ctrllock_state_get() == CTLOCK_INIT) ||
			(national808_ctrllock_state_get() == CTLOCK_OK))
		{
			national808_ctrllock_state_set(CTLOCK_BLE_SPK);
			if(vehiclect_runing_state_get() == CARCTL_RUNING_INIT)
			{
				ctlock_msg.lock_type = 0x60;
				ctlock_msg.lock_res.respondcmd = 0;
				ctlock_msg.lock_res.respondsn = 0;
				ctlock_msg.lock_res.respondsta = 0; 
				ctlock_msg.lock_err = 0;
				ret = national808_ctlock_queue_put(&ctlock_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"mble","ctlock_queue_put bluetooth-60 fail!\r\n");
					national808_ctrllock_state_set(CTLOCK_OK);
				}
				else
				{
					if(vehiclect_runing_state_get() == CARCTL_RUNING_INIT)
					{
						vehiclect_runing_state_set(CARCTL_RUNING_SPEAK_LIGHT);/*鸣笛闪灯状态*/
					}
					else
					{
						national808_ctrllock_state_set(CTLOCK_OK);
					}
				}
			}
			else
			{
				national808_ctrllock_state_set(CTLOCK_OK);
			}
		}
		break;
	case 61:  //状态查询
		ctlock_msg.lock_err = 0;
		ctlock_msg.lock_res.respondcmd = 0;
		ctlock_msg.lock_res.respondsn = 0;
		ctlock_msg.lock_res.respondsta = 0;
		ctlock_msg.lock_type = 0x61;
		
		ret = bluetooth_ctlock_res_queue_put(&ctlock_msg);
		if(ret < 1)
		{
			gaoz_log_debug_printf(INFOR_MODE,"mble","bluetooth_ctlock_res_queue_put  fail!\r\n");
		}
		ret = pdFAIL;	
		break;
	case 67:	//闪灯+鸣笛
		if( (national808_ctrllock_state_get() == CTLOCK_INIT) ||
			(national808_ctrllock_state_get() == CTLOCK_OK))
		{
			national808_ctrllock_state_set(CTLOCK_BLE_SPK);
			if(vehiclect_runing_state_get() == CARCTL_RUNING_INIT)
			{
				ctlock_msg.lock_type = 0x67;
				ctlock_msg.lock_res.respondcmd = 0;
				ctlock_msg.lock_res.respondsn = 0;
				ctlock_msg.lock_res.respondsta = 0; 
				ctlock_msg.lock_err = 0;
				ret = national808_ctlock_queue_put(&ctlock_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"mble","ctlock_queue_put bluetooth-60 fail!\r\n");
					national808_ctrllock_state_set(CTLOCK_OK);
				}
				else
				{
					if(vehiclect_runing_state_get() == CARCTL_RUNING_INIT)
					{
						vehiclect_runing_state_set(CARCTL_RUNING_SPEAK_LIGHT);/*鸣笛闪灯状态*/
					}
					else
					{
						national808_ctrllock_state_set(CTLOCK_OK);
					}
				}
			}
			else
			{
				national808_ctrllock_state_set(CTLOCK_OK);
			}
		}
		break;
	case 68:	//闪灯
		if( (national808_ctrllock_state_get() == CTLOCK_INIT) ||
			(national808_ctrllock_state_get() == CTLOCK_OK))
		{
			national808_ctrllock_state_set(CTLOCK_BLE_SPK);
			if(vehiclect_runing_state_get() == CARCTL_RUNING_INIT)
			{
				ctlock_msg.lock_type = 0x68;
				ctlock_msg.lock_res.respondcmd = 0;
				ctlock_msg.lock_res.respondsn = 0;
				ctlock_msg.lock_res.respondsta = 0; 
				ctlock_msg.lock_err = 0;
				ret = national808_ctlock_queue_put(&ctlock_msg);
				if(ret < 1)
				{
					gaoz_log_debug_printf(INFOR_MODE,"mble","ctlock_queue_put bluetooth-60 fail!\r\n");
					national808_ctrllock_state_set(CTLOCK_OK);
				}
				else
				{
					if(vehiclect_runing_state_get() == CARCTL_RUNING_INIT)
					{
						vehiclect_runing_state_set(CARCTL_RUNING_SPEAK_LIGHT);/*鸣笛闪灯状态*/
					}
					else
					{
						national808_ctrllock_state_set(CTLOCK_OK);
					}
				}
			}
			else
			{
				national808_ctrllock_state_set(CTLOCK_OK);
			}
		}
		break;
	default:
		//给手机App发送一个ERROR 02
		send_rsa_error(0x02);
		break;
	}
	gaoz_power_man_wakeup_task_num_stop();
	return 0;
}
/** ****************************************************************************
* @remarks      uint16_t bluetoothposition_data(uint8_t *pDa)
* @brief        蓝牙获取位置信息
* @param[in]     无
* @param[out]    *pDa缓存区
* @return       缓存区长度 
* @attention     
*******************************************************************************/
uint16_t bluetoothposition_data(uint8_t *pDa)
{
	uint8_t idx;
	uint8_t term_id[4]={0};
	uint8_t AuthDa[128]={0};	
    rtc_struct Location_Time;
	gnss_info_struct gnss_info;
	n808_vehicle_data_t data_vehicle;

	vehicle_data_now_get(&data_vehicle);
	gaoz_gnss_info_now_get( &gnss_info );
	gaoz_rtc_sys_get(RTC_TIM_CAL_BCD,&Location_Time);

	//Message_ID
	idx = 0;
	
	//內容
    memset(&AuthDa[idx], 0, 4);//報警
    idx += 4;
	//byte0
	if((data_vehicle.Ignition_status&0x08) == 0x08){
	AuthDa[idx+3] |= 0x01;
	}else{
	AuthDa[idx+3] &= ~(0x01);	
	}
	if(gnss_info.gnss_position_info.pos_flag == 1){
	AuthDa[idx+3] |= (0x02);
	}else{
	AuthDa[idx+3] &= ~(0x02);	
	}
	AuthDa[idx+3] &= ~(0x04);
	AuthDa[idx+3] &= ~(0x08);
	if((data_vehicle.Charge_status&0x01) == 0x01){
	AuthDa[idx+3] &= ~(0x10);
	}else if((data_vehicle.Charge_status&0x02) == 0x02){
	AuthDa[idx+3] |= (0x10);	
	}else{
	AuthDa[idx+3] &= ~(0x10);	
	}
	if((data_vehicle.Ignition_status&0x04) == 0x04){
	AuthDa[idx+3] |= (0x20);		
	}else{
	AuthDa[idx+3] &= ~(0x20);	
	}
	if((data_vehicle.Charge_status&0x03) != 0x00){
	AuthDa[idx+3] |= (0x40);			
	}else{
	AuthDa[idx+3] &= ~(0x40);			
	}
	if((data_vehicle.relay_sta&0x01) != 0x00){
	AuthDa[idx+3] |= (0x80);
	}else{
	AuthDa[idx+3] &= ~(0x80);	
	}	
	//byte1 00：P档； 01：D档； 02 N档； 03 R档
	if((data_vehicle.brake_gear&0x04) == 0x04){
	AuthDa[idx+2] = 0x00;		
	}else if((data_vehicle.brake_gear&0x20) == 0x20){
	AuthDa[idx+2] = 0x02;	
	}else if((data_vehicle.brake_gear&0x10) == 0x10){
	AuthDa[idx+2] = 0x01;	
	}else if((data_vehicle.brake_gear&0x08) == 0x08){
	AuthDa[idx+2] = 0x03;		
	}else {
	AuthDa[idx+2] = 0x02;	
	}
	if(((data_vehicle.Charge_status&0x04) == 0x04) ||
		((data_vehicle.Charge_status&0x08) == 0x08))
	{
	AuthDa[idx+2] |= (1<<2);		
	}
	else{
	AuthDa[idx+2] &= ~(1<<2);		
	}
	AuthDa[idx+2] &= ~(1<<3);
	if((data_vehicle.lock&0x01) == 0x01){
	AuthDa[idx+2] &= ~(1<<4);
	}else{
	AuthDa[idx+2] |= (1<<4);	
	}	
	if((data_vehicle.doors_status&0x3f) != 0)
	{
	AuthDa[idx+2] |= (1<<5);	
	}else{
	AuthDa[idx+2] &= ~(1<<5);		
	}
	if((data_vehicle.doors_status&0x02) == 0x02)
	{
	AuthDa[idx+2] |= (1<<6);		
	}else
	{
	AuthDa[idx+2] &= ~(1<<6);	
	}
	if((data_vehicle.doors_status&0x04) == 0x04){
	AuthDa[idx+2] |= (1<<7);		
	}else{
	AuthDa[idx+2] &= ~(1<<7);		
	}
	//byte2
	if((data_vehicle.doors_status&0x01) == 0x01){
	AuthDa[idx+1] |= (1<<0);		
	}else{
	AuthDa[idx+1] &= ~(1<<0);		
	}
	if((data_vehicle.doors_status&0x10) == 0x10){
	AuthDa[idx+1] |= (1<<1);		
	}else{
	AuthDa[idx+1] &= ~(1<<1);		
	}
	#if USER_HXJH
	if((data_vehicle.lights_status&0xC0) != 0x00){ /*雾灯*/
	AuthDa[idx+1] |= (1<<2);	
	}else{
	AuthDa[idx+1] &= ~(1<<2);	
	}
	#else
	AuthDa[idx+1] |= (1<<2);
	#endif
	if((data_vehicle.lock&0x04) == 0x04){
	AuthDa[idx+1] &= ~(1<<3);	
	}else{
	AuthDa[idx+1] |= (1<<3);	
	}
	if((data_vehicle.doors_status&0x08) == 0x08){
	AuthDa[idx+1] |= (1<<4);		
	}else{
	AuthDa[idx+1] &= ~(1<<4);		
	}
	if((data_vehicle.Ignition_status&0x02) == 0x02){
	AuthDa[idx+1] |= (1<<5);		
	}else{
	AuthDa[idx+1] &= ~(1<<5);	
	}
	if((data_vehicle.brake_gear&0x01) == 0x01){
	AuthDa[idx+1] |= (1<<6);	
	}else{
	AuthDa[idx+1] &= ~(1<<6);	
	}
	if((data_vehicle.other_status&0x01) == 0x01){
	AuthDa[idx+1] |= (1<<7);	
	}else{
	AuthDa[idx+1] &= ~(1<<7);	
	}
	//byte3
	#if USER_HXJH
	if((data_vehicle.win_status&0x10) != 0x00){
	AuthDa[idx+0] |= (1<<0);	
	}else{
	AuthDa[idx+0] &= ~(1<<0);	
	}
	#else
	AuthDa[idx+0] |= (1<<0);
	#endif
	if((data_vehicle.brake_gear&0x02) == 0x02){
	AuthDa[idx+0] |= (1<<1);	
	}else{
	AuthDa[idx+0] &= ~(1<<1);	
	}
	if((data_vehicle.lights_status&0x08) == 0x08){
	AuthDa[idx+0] |= (1<<2);	
	}else{
	AuthDa[idx+0] &= ~(1<<2);	
	}	
	if((data_vehicle.lights_status&0x10) == 0x10){
	AuthDa[idx+0] |= (1<<3);	
	}else{
	AuthDa[idx+0] &= ~(1<<3);	
	}	
	if((data_vehicle.lights_status&0x01) == 0x01){
	AuthDa[idx+0] |= (1<<4);	
	}else{
	AuthDa[idx+0] &= ~(1<<4);	
	}	
	if((data_vehicle.lights_status&0x02) == 0x02){
	AuthDa[idx+0] |= (1<<5);	
	}else{
	AuthDa[idx+0] &= ~(1<<5);	
	}	
	if((data_vehicle.lights_status&0x04) == 0x04){
	AuthDa[idx+0] |= (1<<6);	
	}else{
	AuthDa[idx+0] &= ~(1<<6);	
	}	
	if((data_vehicle.win_status&0x0f) != 0x00){
	AuthDa[idx+0] |= (1<<7);	
	}else{
	AuthDa[idx+0] &= ~(1<<7);	
	}			

    idx += 4; 
    u32toHex4(gnss_info.gnss_position_info.latitude,term_id);
    memcpy(&AuthDa[idx],term_id, 4);	//lat
    idx += 4;
    u32toHex4(gnss_info.gnss_position_info.longitude,term_id);
    memcpy(&AuthDa[idx],term_id, 4);	//long
    idx += 4;
	AuthDa[idx] = (gnss_info.gnss_position_info.speed)>>8;
	AuthDa[idx+1] = (gnss_info.gnss_position_info.speed)%0x100;
	//memset(&AuthDa[idx], 0, 2);//gaocheng
	idx += 2;
 	//memset(&AuthDa[idx], 0, 2);//sudu
	AuthDa[idx] = (data_vehicle.speed)>>8;
	AuthDa[idx+1] = (data_vehicle.speed)%0x100;
	idx += 2;   
    memset(&AuthDa[idx], 0, 2);//fangxiang
	idx += 2;
 	//时间
	AuthDa[idx++] = (uint8_t)(Location_Time.year&0xff);	//YY
	AuthDa[idx++] = Location_Time.month;	//MM
	AuthDa[idx++] = Location_Time.day;
	AuthDa[idx++] = Location_Time.hour;
	AuthDa[idx++] = Location_Time.minute;
	AuthDa[idx++] = Location_Time.second; 

	if(data_vehicle.Electricity != 0)
	{
		u32toHex4(data_vehicle.Endurance_mileage, &AuthDa[idx]);
		AuthDa[idx] = data_vehicle.Electricity;	
	}
	if(data_vehicle.Oil != 0)
	{
		u32toHex4(data_vehicle.Oil,&AuthDa[idx]); //0.1L
	}
    idx += 4; 

	u32toHex4(data_vehicle.Odo*10,&AuthDa[idx]);
    idx += 4; 

	AuthDa[idx++] = (data_vehicle.bat_vol/100)>>8;
	AuthDa[idx++] = (data_vehicle.bat_vol/100);


	u32toHex4(gnss_info.gnss_position_info.sat_num+60000+gnss_info.gnss_position_info.gga_fs*1000,&AuthDa[idx]);
    idx += 4; 

	AuthDa[idx++] = 0; //rssi
    AuthDa[idx++] = 0; //ble 
    AuthDa[idx++] = 0; //alchol
    AuthDa[idx++] = 0; //accelerate 
	
	memcpy(pDa,AuthDa,idx);
	return idx;
}
/** ****************************************************************************
* @remarks      void send_bluetooth_data_to_app(uint8_t * pDa, uint16_t len)
* @brief        发送给手机的内容
* @param[in]     *pDa缓存区，len长度
* @param[out]    无
* @return        无
* @attention     
*******************************************************************************/
void send_bluetooth_data_to_app(uint8_t * pDa, uint16_t len)
{
	int ret = 0;
	
	ret = gaoz_ble_drive_data_write( pDa , len );
	if( ret == pdPASS )
	{   
		gaoz_log_debug_printf(INFOR_MODE,"mble","gaoz_ble_drive_data_write :%s,%d\r\n",pDa,len);
	}
}
/** ****************************************************************************
* @remarks      void send_rsa_error(uint8_t errCode)
* @brief        反馈错误码
* @param[in]     errCode错误码
* @param[out]    无
* @return        无
* @attention    
				如果收到不能解析的数据，返回一个error
				暂时只返回 ERROR 01

				只能输入十进制数0~99
				1: 数据不完整
				2：命令字不支持
				3：时间失效
				4：公钥不匹配(没有)
				5：命令长度不匹配
				6:公钥不匹配(长度非216bytes)
				7：公钥不匹配(base64格式错误)
				8：公钥不匹配(base64长度错误)
				9：私钥不匹配(base64格式错误)
				10：私钥不匹配(长度非128bytes) 
*******************************************************************************/
void send_rsa_error(uint8_t errCode)
{
	uint8_t sData[30];

	memcpy(sData, "ERROR", 5);

	sData[5] = ' ';

	sData[6] = (0x0F & (errCode >> 4)) | 0x30;

	sData[7] = (0x0F & errCode) | 0x30;


	send_bluetooth_data_to_app(sData, 8);
}

/** ****************************************************************************
* @remarks      void send_rsa_ctrlrst(uint8_t cmd, uint8_t result)
* @brief        RSA上报结果不带反馈码；
* @param[in]     cmd命令字 ，result结果
* @param[out]    无
* @return        无
* @attention     
*******************************************************************************/
void send_rsa_ctrlrst(uint8_t cmd, uint8_t result)
{
	uint8_t sData[30];
	uint8_t idx;

	idx = 0;
	sData[idx++] = 0x30;
	sData[idx++] = 0x31;
	sData[idx++] = 0x35;//长度3bytes
	sData[idx++] = '|';
	sData[idx++] = 'T';
	sData[idx++] = '|';
	memcpy(&sData[idx], "\x30\x30\x30\x30\x30\x30\x30\x30", 8);
	idx += 8;
	sData[idx++] = '|';
	//	sData[idx++] = 0x00;
	sData[idx++] = cmd / 10 + 0x30;
	sData[idx++] = cmd % 10 + 0x30;
	sData[idx++] = '|';
	if (result == 0)
	{
		sData[idx++] = 0x30;
	}
	else
	{
		sData[idx++] = 0x31;
	}
	send_bluetooth_data_to_app(sData, idx);
}

/** ****************************************************************************
* @remarks      void send_rsa_ctrlrstcode(uint8_t cmd, uint16_t result_code)
* @brief        RSA上报结果带反馈码；
* @param[in]     cmd命令字 result_code错误码
* @param[out]    无
* @return        无
* @attention     
*******************************************************************************/
#if (OS_PROJECT_DEMAND_VERSION == OS_PROJECT_VERSION_WARMCAR)
//warmcar 用该部分
void send_rsa_ctrlrstcode(uint8_t cmd, uint16_t result_code)
{
	uint8_t sData[50];
	uint8_t idx;
	uint8_t i;

	gaoz_mutex_lock(bluetooth_send_data_mutex);
	idx = 0;
	sData[idx++] = 0x30;
	if((cmd < 80) && ( cmd != 63 ))
	{
		sData[idx++] = 0x31;
		sData[idx++] = 0x35;//长度3bytes
	}
	else
	{
		sData[idx++] = 0x33;
		sData[idx++] = 0x30;//长度3bytes
	}
	sData[idx++] = '|';
	sData[idx++] = 'T';
	sData[idx++] = '|';
	memcpy(&sData[idx], "\x30\x30\x30\x30\x30\x30\x30\x30", 8);
	idx += 8;
	sData[idx++] = '|';
	//	sData[idx++] = 0x00;
	sData[idx++] = cmd / 10 + 0x30;
	sData[idx++] = cmd % 10 + 0x30;
	sData[idx++] = '|';
	if (result_code == 0)
	{
		if((cmd < 80) && ( cmd != 63 ))
		{
			sData[idx++] = 0x30;
		}
		else
		{
			for (i = 0; i < 16; i++)
			{
				sData[idx + i] = 0x30;
			}
			idx += 16;
		}
	}
	else
	{
		if((cmd < 80) && ( cmd != 63 ))
		{
			sData[idx++] = 0x31;
		}
		else
		{
			for (i = 0; i < 16; i++)
			{
				sData[idx + i] = 0x30;
			}
			if ((result_code & 0x01) == 0x1)
			{
				sData[18] = 0x31;
			}
			if ((result_code & 0x02) == 0x2)
			{
				sData[19] = 0x31;
			}
			if ((result_code & 0x04) == 0x4)
			{
				sData[20] = 0x31;
			}
			if ((result_code & 0x08) == 0x8)
			{
				sData[21] = 0x31;
			}
			if ((result_code & 0x10) == 0x10)
			{
				sData[22] = 0x31;
			}
			if ((result_code & 0x20) == 0x20)
			{
				sData[23] = 0x31;
			}
			if ((result_code & 0x40) == 0x40)
			{
				sData[24] = 0x31;
			}
			if ((result_code & 0x80) == 0x80)
			{
				sData[25] = 0x31;
			}
			if ((result_code & 0x100) == 0x100)
			{
				sData[26] = 0x31;
			}
			if ((result_code & 0x200) == 0x200)
			{
				sData[27] = 0x31;
			}
			for (i = 0; i < 16; i++)
			{
				sData[18 + i] |= 0x30;
			}
			idx += 16;
		}
	}
	send_bluetooth_data_to_app(sData, idx); 
	gaoz_mutex_unlock( bluetooth_send_data_mutex );
}
#else 
void send_rsa_ctrlrstcode(uint8_t cmd, uint16_t result_code)
{
	uint8_t sData[50];
	uint8_t idx;
	uint8_t i;

	gaoz_mutex_lock(bluetooth_send_data_mutex);
	idx = 0;
	sData[idx++] = 0x30;
	if((cmd < 80))
	{
		sData[idx++] = 0x31;
		sData[idx++] = 0x35;//长度3bytes
	}
	else
	{
		sData[idx++] = 0x33;
		sData[idx++] = 0x30;//长度3bytes
	}
	sData[idx++] = '|';
	sData[idx++] = 'T';
	sData[idx++] = '|';
	memcpy(&sData[idx], "\x30\x30\x30\x30\x30\x30\x30\x30", 8);
	idx += 8;
	sData[idx++] = '|';
	//	sData[idx++] = 0x00;
	sData[idx++] = cmd / 10 + 0x30;
	sData[idx++] = cmd % 10 + 0x30;
	sData[idx++] = '|';
	if (result_code == 0)
	{
		if((cmd < 80) )
		{
			sData[idx++] = 0x30;
		}
		else
		{
			for (i = 0; i < 16; i++)
			{
				sData[idx + i] = 0x30;
			}
			idx += 16;
		}
	}
	else
	{
		if((cmd < 80))
		{
			sData[idx++] = 0x31;
		}
		else
		{
			for (i = 0; i < 16; i++)
			{
				sData[idx + i] = 0x30;
			}
			if ((result_code & 0x01) == 0x1)
			{
				sData[18] = 0x31;
			}
			if ((result_code & 0x02) == 0x2)
			{
				sData[19] = 0x31;
			}
			if ((result_code & 0x04) == 0x4)
			{
				sData[20] = 0x31;
			}
			if ((result_code & 0x08) == 0x8)
			{
				sData[21] = 0x31;
			}
			if ((result_code & 0x10) == 0x10)
			{
				sData[22] = 0x31;
			}
			if ((result_code & 0x20) == 0x20)
			{
				sData[23] = 0x31;
			}
			if ((result_code & 0x40) == 0x40)
			{
				sData[24] = 0x31;
			}
			if ((result_code & 0x80) == 0x80)
			{
				sData[25] = 0x31;
			}
			if ((result_code & 0x100) == 0x100)
			{
				sData[26] = 0x31;
			}
			if ((result_code & 0x200) == 0x200)
			{
				sData[27] = 0x31;
			}
			for (i = 0; i < 16; i++)
			{
				sData[18 + i] |= 0x30;
			}
			idx += 16;
		}
	}
	send_bluetooth_data_to_app(sData, idx); 
	gaoz_mutex_unlock( bluetooth_send_data_mutex );
}
#endif
/** ****************************************************************************
* @remarks     void send_bluetooth_car_statues(uint8_t cmd, uint8_t result)
* @brief       App查询车辆状态应答；
* @param[in]     cmd命令字 ，result结果 
* @param[out]    无
* @return        无
* @attention     
*******************************************************************************/
void send_bluetooth_car_statues(uint8_t cmd, uint8_t result)
{
	uint8_t sData[150];
	//uint8_t tmpDa[128];
	uint8_t idx;
	uint8_t idxST;	//st的下标
	
	uint8_t *pST = (uint8_t *)gaoz_fibo_malloc(1024*1);

	if(pST == NULL)
	{
		return;
	}

	//bluetoothposition_data(pST);
	uploadposition(pST);
	idx = 3;
	
	sData[idx++] = '|';
	sData[idx++] = 'T';
	sData[idx++] = '|';
	memcpy(&sData[idx], "\x30\x30\x30\x30\x30\x30\x30\x30", 8);
	idx += 8;
	sData[idx++] = '|';
	sData[idx++] = cmd / 10 + 0x30;
	sData[idx++] = cmd % 10 + 0x30;
	sData[idx++] = '|';
	if (result == 0)
	{
		sData[idx++] = 0x30;
	}
	else
	{
		sData[idx++] = 0x31;
	}

	//Write_Location_sp_ap_LowerLevel(0, tmpDa, &reLen);

	//下面这些写法有问题，如果后期增删位置信息上报，会影响这里
	idxST = 0;

	//报警标志
	sData[idx++] = ',';
	hexStr2ASCII(&pST[idxST], &sData[idx], 4);
	idx += 8;
	idxST += 4;
	//状态
	sData[idx++] = ',';
	pST[idxST+3] &= 0xFE;	/*去掉ACC状态*/
	hexStr2ASCII(&pST[idxST], &sData[idx], 4);
	idx += 8;
	idxST += 4;
	//纬度
	sData[idx++] = ',';
	hexStr2ASCII(&pST[idxST], &sData[idx], 4);
	idx += 8;
	idxST += 4;

	//经度
	sData[idx++] = ',';
	hexStr2ASCII(&pST[idxST], &sData[idx], 4);
	idx += 8;
	idxST += 4;

	//高程
	sData[idx++] = ',';
	hexStr2ASCII(&pST[idxST], &sData[idx], 2);
	idx += 4;
	idxST += 2;

	//速度
	sData[idx++] = ',';
	hexStr2ASCII(&pST[idxST], &sData[idx], 2);
	idx += 4;
	idxST += 2;

	//方向
	sData[idx++] = ',';
	hexStr2ASCII(&pST[idxST], &sData[idx], 2);
	idx += 4;
	idxST += 2;

	//时间
	sData[idx++] = ',';
	hexStr2ASCII(&pST[idxST], &sData[idx], 6);
	idx += 12;
	idxST += 6;

	//剩余电量soc
	sData[idx++] = ',';
	hexStr2ASCII(&pST[idxST], &sData[idx], 1);
	idx += 2;
	idxST += 1;

	//剩余续航里程
	sData[idx++] = ',';
	hexStr2ASCII(&pST[idxST], &sData[idx], 3);
	idx += 6;
	idxST += 3;

	//总里程
	sData[idx++] = ',';
	hexStr2ASCII(&pST[idxST], &sData[idx], 4);
	idx += 8;
	idxST += 4;

	//当前车辆电压
	sData[idx++] = ',';
	hexStr2ASCII(&pST[idxST], &sData[idx], 2);
	idx += 4;
	idxST += 2;

	//区域ID
	sData[idx++] = ',';
	hexStr2ASCII(&pST[idxST], &sData[idx], 4);
	idx += 8;
	idxST += 4;

	//rssi
	sData[idx++] = ',';
	sData[idx++] = pST[idxST] / 10 + 0x30;
	sData[idx++] = pST[idxST++] % 10 + 0x30;


	//在线模式
	sData[idx++] = ',';
	sData[idx++] = pST[idxST] / 100 + 0x30;
	sData[idx++] = (pST[idxST] % 100) / 10 + 0x30;
	sData[idx++] = (pST[idxST++] % 100) % 10 + 0x30;

	//酒精度
	sData[idx++] = ',';
	sData[idx++] = pST[idxST] / 100 + 0x30;
	sData[idx++] = (pST[idxST] % 100) / 10 + 0x30;
	sData[idx++] = (pST[idxST++] % 100) % 10 + 0x30;

	//预留
	sData[idx++] = ',';
	sData[idx++] = pST[idxST] / 100 + 0x30;
	sData[idx++] = (pST[idxST] % 100) / 10 + 0x30;
	sData[idx++] = (pST[idxST++] % 100) % 10 + 0x30;

	sData[0] = (idx - 4) / 100 + 0x30;
	sData[1] = (idx - 4) % 100 / 10 + 0x30;
	sData[2] = (idx - 4) % 100 % 10 + 0x30;//长度3bytes

	send_bluetooth_data_to_app(sData, idx);

	gaoz_fibo_free(pST);
}

/** ****************************************************************************
* @remarks       void bluetooth_ctlock_res_queue_create( void )
* @brief         控锁异步消息队列创建
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void bluetooth_ctlock_res_queue_create( void )
{
	bluetooth_ctlock_queue = gaoz_queue_create( BT_ACK_QUEUE_MAX_NUM, sizeof( national808_ctrllock_msg_t ) );
    if( bluetooth_ctlock_queue == 0 )
    {
        while( 1 );
    }
}
/** ****************************************************************************
* @remarks       uint32_t_t bluetooth_ctlock_res_queue_get( national_net_res_msg_t *res_msg , uint32_t_t delay_time )
* @brief         获取控锁消息队列
* @param[in]     *res_msg 获取的数据地址
* @param[in]     delay_time 等待的时间
* @param[out]    无
* @return        pdPASS 接收数据成功 ，pdFAIL 接收数据失败
* @attention     无
*******************************************************************************/
static uint32_t bluetooth_ctlock_res_queue_get( national808_ctrllock_msg_t *res_msg , uint32_t delay_time )
{
    if( bluetooth_ctlock_queue == 0 )
    {
        bluetooth_ctlock_res_queue_create();
    }
    if(gaoz_queue_get( bluetooth_ctlock_queue, res_msg, delay_time )  == 0 ) //等待回应信号量
    {
        return pdPASS;
    }

    return pdFAIL;
}

/** ****************************************************************************
* @remarks       uint8_t_t bluetooth_ctlock_res_queue_put( national_net_res_msg_t *res_msg )
* @brief         发送消息队列
* @param[in]     *res_msg 发送的数据地址
* @param[out]    无
* @return        1 发送数据成功 ，0 发送数据失败
* @attention     无
*******************************************************************************/
uint8_t bluetooth_ctlock_res_queue_put( national808_ctrllock_msg_t *res_msg )
{
    if( gaoz_queue_put( bluetooth_ctlock_queue, ( void * )res_msg, 1000 ) == 0 )
    {
        return 1;
    }
	else
	{
		return 0;
	}
}
/** ****************************************************************************
* @remarks       void national_data_analytic_fun( national_response_e *nat_res)
* @brief         蓝牙应答状态分析
* @param[in]     *bluetooth_res 控锁命令字
* @param[out]    无
* @return        无
* @attention     
*******************************************************************************/
static void bluetooth_send_analytic_fun( national808_ctrllock_msg_t  *bluetooth_res)
{
	switch( bluetooth_res->lock_type )
	{	
		case 0x82:	
			send_rsa_ctrlrstcode(82, bluetooth_res->lock_err);
		break;
		case 0x62:	
			send_rsa_ctrlrstcode(62, bluetooth_res->lock_err);
		break;
		case 0x83:	
			send_rsa_ctrlrstcode(83, bluetooth_res->lock_err);
		break;
		case 0x63:	
			send_rsa_ctrlrstcode(63, bluetooth_res->lock_err);
		break;
		case 0x60:	
			send_rsa_ctrlrstcode(60, bluetooth_res->lock_err);
		break;
		case 0x68:	
			send_rsa_ctrlrstcode(68, bluetooth_res->lock_err);
		break;
		case 0x67:	
			send_rsa_ctrlrstcode(67, bluetooth_res->lock_err);
		break;
		case 0x61:
			send_bluetooth_car_statues(61, bluetooth_res->lock_err);
		break;
        default:
            break;
	}
} 
/** ****************************************************************************
* @remarks       static uint32_t_t bluetooth_wait_response_result( uint32_t_t wait_time )
* @brief         等待获取响应消息
* @param[in]     wait_time 延时的时间设置 单位ms
* @param[out]    无
* @return        pdPASS 成功 pdFAIL 失败
* @attention     无
*******************************************************************************/
static uint32_t bluetooth_wait_response_result( uint32_t wait_time )
{
	uint32_t ret = pdFAIL;
	national808_ctrllock_msg_t bluetooth_res_msg;
	
	if( bluetooth_ctlock_res_queue_get( &bluetooth_res_msg , wait_time ) == pdPASS )
	{	
		gaoz_log_debug_printf(INFOR_MODE,"mble","bluetooth_ctlock_res_queue_get ok type:%x,errcode:%x\r\n",bluetooth_res_msg.lock_type,bluetooth_res_msg.lock_err);	
		bluetooth_send_analytic_fun( &bluetooth_res_msg );
		ret = pdPASS;
	}
	else
	{
		ret = pdFAIL;
	}
	return ret;
}
/** ****************************************************************************
* @remarks       static void bluetooth_wait_response_overtime( void )
* @brief         等待响应超时
* @param[in]    
* @param[out]    无
* @return       	
* @attention     无
*******************************************************************************/
static void bluetooth_wait_response_overtime( void )
{
	if((national808_ctrllock_state_get() == CTLOCK_BLE_UNLOCK) ||
	   (national808_ctrllock_state_get() == CTLOCK_BLE_LOCK) ||
	   (national808_ctrllock_state_get() == CTLOCK_BLE_SPK)
	  )
	  {
		  /* 超时 */
		 log_debug_printf(ERROR_MODE,BT_SEND_NAME ,"[%s]bluetooth_wait_response_overtime\r\n",sys_err_infor_pointer_get( 53 ));	
	  }
}
/** ****************************************************************************
* @remarks       static void bluetooth_ctl_send_handle_task( void * pvParameters )
* @brief         蓝牙控制发送任务
* @param[in]     * pvParameters 任务参数
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void bluetooth_ctl_send_handle_task( void * pvParameters )
{
	( void ) pvParameters;
	bluetooth_ctlock_res_queue_create();
	bluetooth_send_data_mutex_create();

	gaoz_mon_reg_task_dog( 60 * GAOZ_TICK_PERIOD_S ,(uint8_t * )BT_SEND_NAME ,TASK_SEM_YES);
    for( ;; )
    {
        gaoz_mon_reset_task_dog();
		if(bluetooth_wait_response_result(1000) == pdPASS)
		{
			/*reverse*/
			national808_ctrllock_state_set(CTLOCK_OK);
		}
		else
		{
			//send_rsa_ctrlrstcode(62,1);
			bluetooth_wait_response_overtime();
		}
	}
}
/** ****************************************************************************
* @remarks       static uint32_t_t gaoz_ble_dev_data_read( uint32_t_t wait_time )
* @brief         读取缓存的数据并组包数据
* @param[in]     wait_time 延时的时间设置 单位ms
* @param[out]    无
* @return        pdPASS 成功 pdFAIL 失败
* @attention     无
*******************************************************************************/
static uint32_t bluetooth_dev_data_read( uint8_t *data, uint16_t len )
{
	uint32_t ret = pdFAIL;
	
	if(data == NULL)
	{
		return ret;
	}
	if ((tagBT.irt_rcv_idx + len) > BT_RCVMAXLEN)
	{
		tagBT.irt_rcv_idx = 0;
	}
	else
	{
	
		memcpy(&tagBT.irt_rcv_buf[tagBT.irt_rcv_idx], data, len);
		tagBT.irt_rcv_idx += len;
	}
	//收购172字节，则认为是完整一包有效数据
	if (tagBT.irt_rcv_idx >= 172)
	{
		if (tagBT.irt_deal_Len == 0)
		{
			tagBT.irt_deal_Len = tagBT.irt_rcv_idx;
			memcpy(tagBT.irt_deal_buf, tagBT.irt_rcv_buf, tagBT.irt_deal_Len);
			ret = pdPASS;
		}
		tagBT.irt_rcv_idx = 0;
	}

	return ret;
}
/** ****************************************************************************
* @remarks       static void net_rece_handle_task( void * pvParameters )
* @brief         蓝牙接收任务
* @param[in]     * pvParameters 任务参数 
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void bluetooth_ctl_recv_handle_task( void * pvParameters )
{   
    ( void )pvParameters;
	int ret = 0;
	uint8_t *read_buf = NULL;
	uint16_t read_buflen = 0;
	uint8_t respda[256]={0};
	uint8_t plainDa[29] = {0};	//多定义一个字节，防止底层使用string函数

	//初始化结构体
	memset(&tagBT, 0, sizeof(bluetooth_recv_t));
	
	do
	{
		ret = file_termpara_read(RSAPARA,(INT8 *)respda);
		gaoz_task_sleep(1000);
		#if DEBUG_BT
		gaoz_log_debug_printf(INFOR_MODE,"mble","filelog_init(RSAPARA)= %d\r\n", ret);
		#endif
	}while(ret < 0);
	
	bluetooth_setrsa(&respda[2],hex2CombineOne(respda));
	#if DEBUG_BT
	gaoz_log_debug_printf(INFOR_MODE,"mble","setBTRSA(RSAPARA)= %s,%d\r\n", tagBT.RSA_PubicKey,tagBT.RSA_PubicKeyLen);
	#endif
	bluetooth_setname(&respda[hex2CombineOne(respda)+2+1],respda[hex2CombineOne(respda)+2]);
	#if DEBUG_BT
	// gaoz_log_debug_printf(INFOR_MODE,"mble","setBTName(RSAPARA)= %s,%d,%d\r\n", tagBT.bluetooth_name,tagBT.bluetooth_nameLen,strlen((char *)bluetooth_getname()));
	#endif
	
	ret = gaoz_ble_drive_data_open( (char *)tagBT.bluetooth_name, bluetooth_recv_state_cb);
	if( ret == pdPASS )
	{
		gaoz_log_debug_printf(INFOR_MODE,"mble","gaoz_ble_drive_data_open( )= %s\r\n", tagBT.bluetooth_name);
	}

	read_buf = (uint8_t *)gaoz_fibo_malloc( BT_RECV_DATA_LEN );
	gaoz_mon_reg_task_dog( 60 * GAOZ_TICK_PERIOD_S ,(uint8_t * )BT_RECV_NAME ,TASK_SEM_YES);
   
    for( ;; )
    {
        gaoz_mon_reset_task_dog();
		//fibo_taskSleep(1000); 
        if( gaoz_ble_drive_data_read( (uint8_t *)read_buf , &read_buflen , 1000 ) == pdPASS )
        {
			gaoz_power_man_wakeup_task_num_run(); 
			//RSA 解析
			if(bluetooth_dev_data_read( read_buf,read_buflen ) == pdPASS)
			{
				ret = BLEanalyRSA(tagBT.irt_deal_buf, tagBT.irt_deal_Len,tagBT.RSA_PubicKey, tagBT.RSA_PubicKeyLen, plainDa);
				if (ret == 0)
				{
					plainDa[28] = 0;
					rsa_decipherdata(plainDa, 28);
				}
				else
				{
					//报个错误
					gaoz_log_debug_printf(INFOR_MODE,"mble","BLEanalyRSA err");
					log_debug_printf(ERROR_MODE,BT_RECV_NAME ,"[%s]BLEanalyRSA  fail\r\n",sys_err_infor_pointer_get( 52 ));
				}
				tagBT.irt_deal_Len = 0;
       		}
			gaoz_power_man_wakeup_task_num_stop();
		}
        else
        {
			if((tagBT.irt_deal_Len != 0) || (tagBT.irt_rcv_idx  != 0))
			{
				gaoz_power_man_wakeup_task_num_run();
				if(tagBT.irt_rcv_idx  != tagBT.irt_rcv_idx_old)
				{
					tagBT.irt_rcv_idx_old = tagBT.irt_rcv_idx;
					tagBT.irt_rcv_sta = 0;
				}
				else 
				{

				}
				tagBT.irt_rcv_sta ++;
				if(tagBT.irt_rcv_sta > 2)
				{
					tagBT.irt_deal_Len = 0;
					tagBT.irt_rcv_sta = 0;
					tagBT.irt_rcv_idx = 0;
					memset(read_buf,0,BT_RECV_DATA_LEN);
					gaoz_log_debug_printf(INFOR_MODE,"mble","irt_deal_Len 0");
				}	
				gaoz_power_man_wakeup_task_num_stop();			
			}	
        }
		
	}
}
/** ****************************************************************************
* @remarks       void bluetooth_ctl_create_task( unsigned portBASE_TYPE uxPriority )
* @brief         应用蓝牙任务
* @param[in]     uxPriority 优先级
* @param[out]    无
* @return        无
* @attention     在系统启动后进行创建，因涉及到rsa解密，会调用tmath中的运算，栈空间适当大一点;
*******************************************************************************/
void bluetooth_ctl_create_task( unsigned portBASE_TYPE uxPriority )
{
    gaoz_thread_create(bluetooth_ctl_send_handle_task, BT_SEND_NAME, 1024*2, NULL, uxPriority);
    gaoz_thread_create(bluetooth_ctl_recv_handle_task, BT_RECV_NAME, 1024*30, NULL, uxPriority);
}
